package com.lagou.resume.controller;

import com.alibaba.fastjson.JSON;
import com.lagou.resume.pojo.Resume;
import com.lagou.resume.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 简历的控制类
 * @autor yangzhenhao
 */
@Controller
public class ResumeController {

    @Autowired
    private IResumeService resumeService;

    /**
     * 跳转到简历列表页面的方法
     * @return
     */
    @RequestMapping("/toResumeList")
    public String toResumeList() {
        return "resume_list";
    }

    /**
     * 查询所有简历记录的方法
     * @return
     */
    @RequestMapping(value = "/findAll",produces = "text/json;charset=utf-8")
    @ResponseBody
    public String findAll() {
        List<Resume> resumeList = resumeService.findAll();
        return JSON.toJSONString(resumeList);
    }

    /**
     * 根据ID删除简历记录的方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteByID",produces = "text/json;charset=utf-8")
    @ResponseBody
    public String deleteByID(Long id) {
        Map<String,String> map = new HashMap<>();
        try {
            resumeService.deleteByID(id);
            map.put("code","0");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 保存或更新简历记录的方法
     * @param resume
     * @return
     */
    @RequestMapping(value = "/saveOrUpdate",produces = "text/json;charset=utf-8")
    @ResponseBody
    public String saveOrUpdate(Resume resume) {
        Map<String,String> map = new HashMap<>();
        try {
            resumeService.saveOrUpdate(resume);
            map.put("code","0");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code","1");
        }
        return JSON.toJSONString(map);
    }

}
